﻿using FinalTestMachine.Motion.Product;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace FinalTestMachine.View.MES
{
    /// <summary>
    /// Wnd_MesTest.xaml 的交互逻辑
    /// </summary>
    public partial class Wnd_MesTest : Window
    {
        public Wnd_MesTest()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 日志的信息集合
        /// </summary>
        ObservableCollection<FinalTestMesHelper> MesInfo_list = new ObservableCollection<FinalTestMesHelper>();
        private void bt_ChecKSN_Click(object sender, RoutedEventArgs e)
        {
            if (FinalTestMesHelper.Cur.CheckSN(this.tb_MesSn.Text))
            {
                FinalTestMesHelper.IsCheckSnOK = false;
                MessageBox.Show("SN检查成功！");
            }
            else
            {
                MessageBox.Show($"SN检查失败,{FinalTestMesHelper.Cur.ErrMesInfo}");
            }

        }

        void Sleep(int str)
        {
            Thread.Sleep(str);
        }

        private void bt_DataUp_Click(object sender, RoutedEventArgs e)
        {
            ProductInfo productTrayinfo = new ProductInfo();
            productTrayinfo.Number = "1230";
            productTrayinfo.Result = "OK";
            productTrayinfo.DataUpInfo = "123@20@30@21@OK|421@20@30@21@OK";
            productTrayinfo.SetCurrentTimeToMesStartTime();
            Sleep(1000);
            productTrayinfo.SetCurrentTimeToMesEndTime();
            if (FinalTestMesHelper.Cur.DataUp(productTrayinfo))
            {
                MessageBox.Show("数据上传成功");
            }
            else
            {
                MessageBox.Show("数据上传失败"+FinalTestMesHelper.Cur.ErrMesInfo);
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.image_test.SetBinding(TextBox.TextProperty, new Binding("TestImage") { Source = FinalTestMesHelper.Cur });
            this.image_result.SetBinding(TextBox.TextProperty, new Binding("ResultImage") { Source = FinalTestMesHelper.Cur });
            lv_log.ItemsSource = MesInfo_list;
            //日志事件注册
            FinalTestMesHelper.MesInfoEventHandler += WriteMesInfo;
        }
        void WriteMesInfo(string msgItem, Brush brushColor)
        {
            Task.Run(() =>
            {
                this.Dispatcher.Invoke(() =>
                {
                    if (MesInfo_list.Count >= 30)
                    {
                        MesInfo_list.Clear();
                    }
                    FinalTestMesHelper item = FinalTestMesHelper.Cur;
                    MesInfo_list.Add(item);
                    //翻动到最新的一行信息
                    lv_log.ScrollIntoView(item);
                });
            });

        }

        private void btn_Testsearch_Click(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
                folderBrowserDialog.ShowDialog();
                this.image_test.Text = folderBrowserDialog.SelectedPath.ToString();
                this.image_test.Focus();
            });
        }

        private void btn_Resultsearch_Click(object sender, RoutedEventArgs e)
        {
            this.Dispatcher.Invoke(() =>
            {
                
                System.Windows.Forms.FolderBrowserDialog folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
                folderBrowserDialog.ShowDialog();
                this.image_result.Text = folderBrowserDialog.SelectedPath.ToString();
                this.image_result.Focus();
            });
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            try
            {
                FinalTestMesHelper.Cur.Save();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
    }
}
